<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Mmod_pitcalcul extends MYAD_Model{
		protected $table_name = 'm_pitcalculationrule';
        function __construct(){
			parent::__construct();
		}
       
                
        /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function checkLevel 
     * @param: ParameterId          
     * @Output: int
     * @This function return max level of parameterID
     */ 
        function checkLevel($ParameterId) {
            // fetch first row in object
            $this->db->select_max('Level');
            $this->db->where('ParameterId', $ParameterId);
            $result = $this->db->get($this->table_name)->row();              
            return $result->Level+1;
        }   
        
        /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function checkAssociatedAmount 
     * @param: AssociatedAmount          
     * @Output: TRUE or FALSE
     * @This function check AssociatedAmount 
     */ 
        function checkAssociatedAmount($ParameterId=NULL,$AssociatedAmount=NULL) {
            $this->db->select_max('AssociatedAmount');
            $this->db->where('ParameterId', $ParameterId);            
            $result = $this->db->get($this->table_name)->row();  
            if($result->AssociatedAmount>=$AssociatedAmount){
                    $data= array(
                                    'flag' => FALSE,
                                    'message' => $this->lang->line('pcrr__AssociatedAmountGreeter1').$result->AssociatedAmount.$this->lang->line('pcrr__AssociatedAmountGreeter2')
                                );
            }else{      
                $data['flag']=TRUE;                
            }
            return $data;
        }
        
        /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function checkAssociatedAmountLevel 
     * @param: $ParameterId,$AssociatedAmount,$Level          
     * @Output: TRUE or FALSE
     * @This function check AssociatedAmount 
     */ 
        function checkAssociatedAmountLevel($ParameterId=NULL,$AssociatedAmount=NULL,$Level) {
            //find min
            $this->db->select_max('AssociatedAmount');
            $this->db->where('ParameterId', $ParameterId);            
            $this->db->where('Level', $Level-1);            
            $result = $this->db->get($this->table_name)->row();  
            $minAssociatedAmount= $result->AssociatedAmount;
            //find max
            $this->db->select_max('AssociatedAmount');
            $this->db->where('ParameterId', $ParameterId);            
            $this->db->where('Level', $Level+1);            
            $result = $this->db->get($this->table_name)->row();  
            $maxAssociatedAmount= $result->AssociatedAmount;                     
            //compare            
                if(($maxAssociatedAmount>$AssociatedAmount && $minAssociatedAmount<$AssociatedAmount) || ((sizeof($maxAssociatedAmount)<1) && ($minAssociatedAmount<$AssociatedAmount))){
                    $data['flag']= TRUE;
                }elseif((sizeof($maxAssociatedAmount)<1) && ($minAssociatedAmount>=$AssociatedAmount)){   
                        $data= array(
                                    'flag' => FALSE,
                                    'message' => $this->lang->line('pcrr__AssociatedAmountGreeter1').$minAssociatedAmount.$this->lang->line('pcrr__AssociatedAmountGreeter2')
                                );
                }elseif((sizeof($minAssociatedAmount)<1) && ($maxAssociatedAmount<=$AssociatedAmount)){ 
                        $data= array(
                                    'flag' => FALSE,
                                    //'message' => $this->lang->line('pcrr__AssociatedAmountGreeter1').$minAssociatedAmount.$this->lang->line('pcrr__AssociatedAmountGreeter2')
                                    'message' =>$this->lang->line('pcrr__AssociatedAmountLess1').$maxAssociatedAmount.$this->lang->line('pcrr__AssociatedAmountLess2')
                                );
                }else{ 
                            $data= array(
                                            'flag' => FALSE,
                                            'message' => $this->lang->line('pcrr__AssociatedAmountBeetween1').$minAssociatedAmount.$this->lang->line('pcrr__AssociatedAmountBeetween2').$maxAssociatedAmount.$this->lang->line('pcrr__AssociatedAmountBeetween3')
                                        );
         
                }
            return $data;
        }
        
        /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function checkRate 
     * @param: Rate          
     * @Output: TRUE or FALSE
     * @This function check Rate 
     */ 
        function checkRate($ParameterId=NULL,$Rate=NULL) {
            $this->db->select_max('Rate');
            $this->db->where('ParameterId', $ParameterId);            
            $result = $this->db->get($this->table_name)->row();  
            if($result->Rate>=$Rate){ 
                    $data= array(
                                    'flag' => FALSE,
                                    'message' => $this->lang->line('pcrr__RateGreeter1').$result->Rate*100 .$this->lang->line('pcrr__RateGreeter2')
                                ); 
            }else{      
                $data['flag']=TRUE; 
            }
            return $data;
        }
        
         /**
     * @author Thanh NV        
     * @created:2014/06/23
     * @function checkRateLevel 
     * @param: $ParameterId,$Rate,$Level          
     * @Output: TRUE or FALSE
     * @This function check Rate
     */ 
        function checkRateLevel($ParameterId=NULL,$Rate=NULL,$Level) {
            //find min  near current
            $this->db->select_max('Rate');
            $this->db->where('ParameterId', $ParameterId);            
            $this->db->where('Level', $Level-1);            
            $result = $this->db->get($this->table_name)->row();  
            $minRate= $result->Rate;
            //find max current
            $this->db->select_max('Rate');
            $this->db->where('ParameterId', $ParameterId);            
            $this->db->where('Level', $Level+1);            
            $result = $this->db->get($this->table_name)->row();  
            $maxRate= $result->Rate;           
            //compare
            if(($maxRate>$Rate && $minRate<$Rate) || ((sizeof($maxRate)<1) && ($minRate<$Rate))){
                $data['flag']=TRUE; 
            }elseif((sizeof($maxRate)<1) && ($minRate>=$Rate)){   
                        $data= array(
                                    'flag' => FALSE,
                                    'message' => $this->lang->line('pcrr__RateGreeter1'). $minRate*100 .$this->lang->line('pcrr__RateGreeter2')
                                );
            }elseif((sizeof($minRate)<1) && ($maxRate<=$Rate)){ 
                        $data= array(
                                    'flag' => FALSE,                                    
                                    'message' =>$this->lang->line('pcrr__RateLess1'). $maxRate*100 .$this->lang->line('pcrr__RateLess2')
                                );    
            }else{
                    $data= array(
                                    'flag' => FALSE,
                                    'message' => $this->lang->line('pcrr__RateBeetween1').$minRate*100 .$this->lang->line('pcrr__RateBeetween2').$maxRate*100 .$this->lang->line('pcrr__RateBeetween3')
                                );              
            }
            return $data;
        }
}       
        
?>